package com.boxuegu.caihuaxuan24.myapplication.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.boxuegu.caihuaxuan24.myapplication.R;
import com.boxuegu.caihuaxuan24.myapplication.activity.ExerciseDetailActivity;
import com.boxuegu.caihuaxuan24.myapplication.model.ExerciseModel;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;


/**

 * A simple {@link Fragment} subclass.

 * Activities that contain this fragment must implement the

 * to handle interaction events.

 * create an instance of this fragment.

 */

public class exerciseFragment extends Fragment {

    ListView mListview;
    MyAdapter myAdapter;
    public ArrayList<ExerciseModel.Date> lists = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragment_exercise, container, false);
        mListview= view.findViewById(R.id.listview);
        myAdapter = new MyAdapter();
        mListview.setAdapter(myAdapter);
        RequestParams requestParams = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/exerciselist");

        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Gson gson  = new Gson();
                ExerciseModel exerciseModel=  gson.fromJson(result, ExerciseModel.class);
                Log.i("ExerciseFragment",result);
                if (exerciseModel != null &&exerciseModel.data != null) {
                    lists = exerciseModel.data;
                    myAdapter.notifyDataSetInvalidated();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("ExerciseFragment",ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });




        return  view;
    }

    public class  MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ExerciseModel.Date date = lists.get(position);
            ViewHolder viewHolder;
            if (convertView == null){
                viewHolder = new ViewHolder();
                convertView = View.inflate(getActivity(),R.layout.list_item_layout,null);
                viewHolder.mPostionTv = convertView.findViewById(R.id.postiontext);

                viewHolder.titleTv =convertView.findViewById(R.id.titletext);
                viewHolder.countTv =convertView.findViewById(R.id.count);
                viewHolder.rootLayout = convertView.findViewById(R.id.root);
                convertView.setTag(viewHolder);


            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.mPostionTv.setText(position+1+"");
            viewHolder.rootLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(getActivity(), ExerciseDetailActivity.class);
                    intent.putExtra("id",date.id);
                    startActivity(intent);

                }
            });
            viewHolder.titleTv.setText(date.title);
            viewHolder.countTv.setText("共计5题");
            return convertView;
        }

        public class ViewHolder {
            public TextView mPostionTv;
            public TextView titleTv;
            public TextView countTv;
            public ConstraintLayout rootLayout;


        }
    }





}
